package LUCKY.Android.DataAccess;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


import LUCKY.Android.valoresGenerales;
import LUCKY.Android.Util.FunctionsUtil;
import LUCKY.Android.Util.FunctionsUtil.TipoLog;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import com.google.code.microlog4android.Logger;
import com.google.code.microlog4android.LoggerFactory;

public class DatabaseHelper extends SQLiteOpenHelper{
	private static final String CLASS_NAME = "DatabaseHelper";
	private static final Logger logger = LoggerFactory.getLogger();

	public DatabaseHelper(Context context) {
		super(context, valoresGenerales.DATABASE_NAME, null, valoresGenerales.DATABASE_VERSION);
		
	}


	

	
	//manejo de versiones basado en 
	//http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android-apps/4.0.1_r1/com/android/providers/calendar/CalendarDatabaseHelper.java/?v=source
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String sql="";
		FunctionsUtil.generaLog(TipoLog.Debug, CLASS_NAME, "onUpgrade", logger,"oldVersion: " + oldVersion);
		FunctionsUtil.generaLog(TipoLog.Debug, CLASS_NAME, "onUpgrade", logger,"newVersion: " + newVersion);
	
		
			//if (oldVersion==3 && newVersion==4) {
			if (oldVersion<2) {
				
			}

						
	}
	
	
	
	
	
	
	
	public boolean importDatabase(String dbPath, String nombrebd) throws IOException {

	    // Close the SQLiteOpenHelper so it will commit the created empty
	    // database to internal storage.
	    close();
	    File newDb = new File(dbPath);
	    File oldDb = new File(valoresGenerales.DB_FILEPATH +nombrebd);
	    if (newDb.exists()) {
	        FunctionsUtil.copyFile(new FileInputStream(newDb), new FileOutputStream(oldDb));
	        // Access the copied database so SQLiteHelper will cache it and mark
	        // it as created.
	      // getWritableDatabase().close();
	        FunctionsUtil.ShowMessage("se restauro correctamente",Toast.LENGTH_SHORT);
  			
	        return true;
	    }
	    return false;
	}
	
	  
	
	
	
	
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		//Aqui debemos crear todas las tablas
		db.execSQL(EMPRESATable.DATABASE_CREATE);
		db.execSQL(USUARIOTable.DATABASE_CREATE);
	
		db.execSQL(TIEMPOTABLE.DATABASE_CREATE);
		db.execSQL(TIPOTABLE.DATABASE_CREATE);
		
		db.execSQL(DISPONIBILIDADTABLE.DATABASE_CREATE);
		db.execSQL(ESPECIETable.DATABASE_CREATE);
		
		db.execSQL(ESTADOTABLE.DATABASE_CREATE);
		db.execSQL(GENEROTABLE.DATABASE_CREATE);
		
		db.execSQL(LOCALIZACIONTABLE.DATABASE_CREATE);
		db.execSQL(EJEMPLARTABLE.DATABASE_CREATE);
		//db.execSQL(CO.DATABASE_CREATE);
		
		
	}


}
